/**
 * Created by bian on 16-2-18.
 */

require('./base.js').bottomBar(0);
require('../../css/phone/home.scss');
require('../../bower_components/skippr/dist/skippr.css');

var $ = jQuery;
require('imports?$=jquery!../../bower_components/jquery-ui/jquery-ui.min.js');
require('imports?$=jquery!../../bower_components/skippr/dist/skippr.js');
require('imports?$=jquery!../../bower_components/jquery.ratemate/lib/raphael-min.js');
require('imports?$=jquery!../../bower_components/jquery.ratemate/src/compiled/jquery.ratemate.js');
require('angular');

$(document).ready(function(){

    $("#theTarget").skippr({
        transition: 'slide',
        speed: 1000,
        easing: 'easeOutQuart',
        navType: 'bubble',
        childrenElementType: 'div',
        arrows: false,
        autoPlay: true,
        autoPlayDuration: 5000,
        keyboardOnAlways: false,
        hidePrevious: false
    });

});

var app = angular.module('app',[]);

var ajaxError = function () {
    alert('操作失败，请刷新重试');
};

app.controller('MainCtrl',['$scope','$http',function(scope,$http){
    scope.slide = function(){
        angular.element('.main').hide('slide',{direction:'left'},400);
        angular.element('.searchPage').show("slide",{ direction: "right" }, 400);
    }
}]);

app.controller('searchCtrl',['$scope','$http',function(scope,$http){
    scope.back = function(){
        angular.element('.main').show("slide",{direction:'left'},400);
        angular.element('.searchPage').hide("slide",{ direction: "right" }, 400);
    };

    scope.shopsSearchData = [];
    scope.searchText = '';
    scope.search = function(){
        if(!scope.searchText){
            return;
        }
        var text = scope.searchText;
        $http
            .get('/user/search/shops?' + text)
            .success(function(data){
                try {
                    for (var x in data) {
                        var Container = JSON.parse(data[x].Container.value);
                        data[x].sellprice = Container.freeLine;
                        data[x].logisticprice = Container.basicFare;
                    }
                }catch(err){
                    console.log(err)
                }
                scope.shopsSearchData = data;
            });
    };
}]);

app.controller('LocationCtrl',['$scope','$http',function(scope,$http){
    var locationScope = scope;
    locationScope.city = '';
    $http
        .get('/user/location')
        .success(function(data){
            locationScope.city = data;
            locationScope.city = locationScope.city.length > 9 ? locationScope.city.substr(0,9) +'..' : locationScope.city;
        });
}]);

app.controller('adsCtrl',['$scope','$http',function(scope,$http){
    var imgsDom = angular.element('#imgs').html();
    scope.imgs = JSON.parse(imgsDom);
}]);

app.directive('repeatFinish',function(){
    return {
        link: function(scope,element,attr){
            if(scope.$last == true){
                scope.$emit('repeatFinished');
            }
        }
    }
});

app.directive('windowHref',function(){
    return {
        link:function(scope,element,attr){
            scope.shopping = function (){
                window.location.href = '/user/index/' + attr.shopid;
            }
        }
    }
});

app.controller('shopsCtrl',['$scope','$http',function(scope,$http){
    var shopsScope = scope;
    $http
        .get('/user/shopData')
        .success(function(data){
            shopsScope.shopsData = data;
            if(shopsScope.shopsData.length !== 0){
                updateShopsData(shopsScope.shopsData);
            }
        }).error(ajaxError);

    function updateShopsData(data){
        for(var x in data){
            var Container = JSON.parse(data[x].Container.value);
            data[x].sellprice = Container.freeLine;
            data[x].logisticprice = Container.basicFare;
        }
        shopsScope.$applyAsync();
    }

    //UI插件初始化
    shopsScope.$on('repeatFinished',function (){
        $(function(){
            $('meter').ratemate({width:78,height:20});
        });
    });

}]);

angular.bootstrap(document.documentElement, ['app']);